

//
//  JBWMainViewController.swift
//  新浪微博
//
//  Created by 季伯文 on 2017/6/28.
//  Copyright © 2017年 bowen. All rights reserved.
//

import UIKit

class JBWMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //自定义tabbar
        let tabBar = JBWTabBar()
        
        //设置代理
        tabBar.jbwDelegate = self
        //实例化闭包
        tabBar.closure = {
            //回调
            print("监听按钮点击")
        }
        //使用kvc 完成只读属性赋值
        setValue(tabBar, forKey: "tabBar")
        
        addChildViewController(JBWHomeViewController(), title:"首页", imageName: "tabbar_home")
        addChildViewController(JBWMessageViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(JBWDiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(JBWProfileViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    /// 添加子控制器方法
    ///
    /// - Parameters:
    ///   - childController: 子控制器
    ///   - title: 标题
    ///   - imageName: 图片名
    func addChildViewController(_ childController: UIViewController, title: String, imageName:String) {
        
        //设置title
        childController.navigationItem.title = title
        childController.tabBarItem.title = title
        
        //选中颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: UIControlState.selected)
        
        //设置图片
        childController.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //选中图片
        childController.tabBarItem.selectedImage = UIImage(named:"\(imageName)_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        
        //导航控制器
        let nav = UINavigationController(rootViewController:childController)
        
        //添加子控制器
        addChildViewController(nav)
    }
}

extension JBWMainViewController: JBWTabBarDelegate {
    func composeButtonSelect() {
        print("按钮点击代理")
        JBWComposeView.show(target: self)
    }
}
